#!/usr/bin/python
import urllib
import urllib2

from gcode_robot import *
from proyectos import projects,coords
from commons import yconfig, gconfig
from youtrack.connection import Connection

def create_project(conn, projectId, projectName, data):
    print "Cargando proyecto %s..." % projectName 
    lead = data.get('lead', 'daniel.gutson@fudepan.org.ar')
    conn.createProjectDetailed(projectId, projectName, '', lead)

def convert(projectName, projectId):
    from googlecode2youtrack import googlecode2youtrack
    googlecode2youtrack(gconfig.login, gconfig.password, yconfig.url, yconfig.login, yconfig.password, projectName, projectId)

def update_gcode(b, projectName):
    set_issue_page(b, projectName)
    youtrack_url = yconfig.url
    template = 'Check our tracker in <a href="%s">%s</a>' % (youtrack_url, youtrack_url)
    create_youtrack_page(b, projectName, template)

def main():
    conn = Connection(yconfig.url, yconfig.login, yconfig.password)
    conn.createGroup('coordinator')
    for coord in coords:
        conn.createUserDetailed(coord, coord, coord, coord)
        conn.setUserGroup(coord, 'coordinator')

    b = make_browser()
    #gmail_login(b, gconfig.login, gconfig.password)

    for project in projects:
        projectName = project['name']
        projectId = project.get('id', projectName.replace('-', ''))
        create_project(conn, projectId, projectName, project)
        convert(projectName, projectId)
        #[Hugo]: precaucion, no descomentar esto hasta la importacion real
        #update_gcode(b, projectName)

if __name__ == "__main__":
    main()
